package club.pchome.controller;

import java.util.Date;
import java.util.List;

import club.pchome.common.constant.SystemConstant;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import club.pchome.dto.Result;
import club.pchome.model.AutoSign;
import club.pchome.model.UserAdmin;
import club.pchome.service.AutoSignService;
import club.pchome.service.UserAdminService;
import club.pchome.service.UserInfoService;
import club.pchome.util.DateUtil;
import club.pchome.util.JsoupSignAdm;
import club.pchome.util.SendSms;

@Api(tags = { "短信接口" })
@RestController
public class UnprocessedController extends BaseController{

	@Autowired
	AutoSignService autoSignService;

	@Autowired
	UserInfoService userInfoService;

	@Autowired
	UserAdminService userAdminService;
	
	@Autowired
	JsoupSignAdm signAdm;
	
	@Autowired
	SendSms sendSms;
	
	/****
	 *  判断有没有待处理任务
	 * @param request
	 * @param userName
	 * @return
	 */
	@PostMapping("/unprocessedList")
	public void unprocessedList(){
		List<UserAdmin> adminList = userAdminService.getUserToHolder();
		for (UserAdmin userAdmin : adminList) {
			List<AutoSign> list = autoSignService.getUserHolder(userAdmin.getHolder());
			if(list.size() > 0) {
				System.out.println("-------------"+userAdmin.getHolder()+"--------------");
				for (AutoSign autoSign : list) {

					String JSESSIONID = ADMLOGIN(autoSign.getPhone() , autoSign.getPwd());

					if(JSESSIONID.equals("10021")){
						logger.error(autoSign.getPhone() + " : PASSWORD_ERROR!!!");
					}else{

						String WaitTaskCount = signAdm.JsoupToDataByCookieAndBody(SystemConstant.WaitTaskCount , "{}" , JSESSIONID);
						JSONObject waitTaskCount = JSONObject.parseObject(WaitTaskCount);
						int _code_2_ = waitTaskCount.getInteger("code");
						int data = waitTaskCount.getInteger("data");
						if(_code_2_ == 200) {

							if(data != 0) {

								String waitTaskCountDetail = new JsoupSignAdm().WaitTaskCountDetail(JSESSIONID);
								JSONObject detail = JSONObject.parseObject(waitTaskCountDetail);

								int detail_code = detail.getInteger("code");
								String detail_data = detail.getString("data");
								JSONObject json_detail = JSONObject.parseObject(detail_data);
								int sale = json_detail.getInteger("sale");
								int buy  = json_detail.getInteger("buy");
								if(detail_code == 200) {

									if(sale > 0) {
										System.out.println("id : "+autoSign.getId() + ", 手机号 : " + autoSign.getPhone() +" : 当前有 "+sale+" 个卖单待处理!!!  data : " + detail_data);
									}
									if(buy > 0) {
										System.out.println("id : "+autoSign.getId() + ", 手机号 : " + autoSign.getPhone() +" : 当前有 "+buy+" 个买单待处理!!!  data : " + detail_data);
									}

								}

							}
						}
					}
				}
			}
		}
	}
	
	/****
	 *  判断有没有待处理任务并发送短信
	 * @param request
	 * @param userName
	 * @return
	 */
	@PostMapping("/unprocessedSendSms")
	public Result<String> unprocessedSendSms(){
		
		List<UserAdmin> adminList = userAdminService.getUserToHolder();
		for (UserAdmin userAdmin : adminList) {
			List<AutoSign> list = autoSignService.getUserHolder(userAdmin.getHolder());
			if(list.size() > 0) {
				String SMS_MSG = "";
				for (AutoSign autoSign : list) {
					//获取cookie
					String JSESSIONID = ADMLOGIN(autoSign.getPhone() , autoSign.getPwd());

					if (JSESSIONID.equals("10021")) {
						logger.error(autoSign.getPhone() + ": PASSWORD_ERROR");

						//-----记录手机号
						autoSign.setPhone(autoSign.getPhone());
						autoSign.setIsture(1);
						autoSign.setIsscan(1);
						autoSignService.updateAccountInfo(autoSign);
					}else{
						String WaitTaskCount = signAdm.JsoupToDataByCookieAndBody(SystemConstant.WaitTaskCount , "{}" , JSESSIONID);
						JSONObject waitTaskCount = JSONObject.parseObject(WaitTaskCount);
						int code = waitTaskCount.getInteger("code");
						int data = waitTaskCount.getInteger("data");
						if(code == 200) {

							if(data != 0) {
								autoSign.setPhone(autoSign.getPhone());
								autoSign.setSmsday(1);
								autoSign.setSmsnum(1);
								long status = autoSignService.updateSMSNum(autoSign);

								if(status > 0) {
									SMS_MSG += autoSign.getPhone() +" : 当前待处理任务 :  "+ data +";\r\n";
								}

							}
						}
					}

				}
				if(!SMS_MSG.equals("")) {
					SMS_MSG += "\r\n 当前时间 : "+DateUtil.hhmmss.format(new Date());
					System.out.println(SMS_MSG);
					boolean b = sendSms.clSendSms(userAdmin.getPhone(), SMS_MSG);
					if(b) {
						System.out.println(userAdmin.getPhone()+ "短信发送成功!");
					}else {
						System.out.println(userAdmin.getPhone()+ "短信发送失败!!!");
					}
					System.out.println("---------------------------");
				}else {
					//System.out.println(userAdmin.getHolder()+" : No Task processed!!!");
				}
				
			}
		}
		
		return Result.initSuccessResult("OK");
	}
}
